Skip to main content

Socket

I use socket io in backend, so you must use socket io in your front not web socket !

note you can use v chat test server base url is http://170.178.195.150:81/api/v1/

connect url is {{baseUrl}} without /api/v1/ so it will be http://170.178.195.150:81

Socket main name space

  • connect data
{
'transports': ['websocket'],
'pingTimeout': 5000,
'connectTimeout': 5000,
'pingInterval': 5000,
'extraHeaders': <String, String>{
'Authorization': accessToken,
"accept-language": currentLocal
},
'forceNew': true
}

accessToken No Bearer token just send the token you can get one from login or register

socket can't connect if no accessToken provided or not valid access token send

socket io events from clint

join emit with acknowledgment

  1. once you success connect you should emitWithAck to join, and you will receive rooms data from callback
  2. response will be like this
  • res is in map format you don't need to decode it
{
"success": true,
"data": [
{
"_id": "61cd92223bd8731be040e11e",
"blockerId": null,
"groupSetting": null,
"updatedAt": 1640862250276,
"creatorId": "61cd8fb33bd8731be040e103",
"roomType": "single",
"lastMessage": {
"_id": "61cd922a3bd8731be040e138",
"messageType": "text",
"messageAttachment": null,
"createdAt": 1640862250273,
"content": "first",
"senderId": "61cd8fb33bd8731be040e103",
"senderName": "user1 updated name",
"senderImageThumb": "REGISTER_3c651b24-3912-4191-a131-af99292a75f9_THUMB.jpg",
"roomId": "61cd92223bd8731be040e11e"
},
"isOnline": 0,
"title": "user2",
"peerId": "61cd8ff33bd8731be040e108",
"peerEmail": "user2",
"thumbImage": "default_user_image.png",
"ifPeerReadMyLastMessage": 0,
"unReadCount": 0,
"roomMembersCount": 2,
"isMute": 0
}
]
}

socket io on events

on update_one_room

  1. this will fire if any room data changed like last message changed or user block me
  2. res is in map format you don't need to decode it
  3. response will like this
{
"_id": "61cd92223bd8731be040e11e",
"blockerId": null,
"groupSetting": null,
"updatedAt": 1640862250276,
"creatorId": "61cd8fb33bd8731be040e103",
"roomType": "single",
"lastMessage": {
"_id": "61cd922a3bd8731be040e138",
"messageType": "text",
"messageAttachment": null,
"createdAt": 1640862250273,
"content": "first",
"senderId": "61cd8fb33bd8731be040e103",
"senderName": "user1 updated name",
"senderImageThumb": "REGISTER_3c651b24-3912-4191-a131-af99292a75f9_THUMB.jpg",
"roomId": "61cd92223bd8731be040e11e"
},
"isOnline": 0,
"title": "user2",
"peerId": "61cd8ff33bd8731be040e108",
"peerEmail": "user2",
"thumbImage": "default_user_image.png",
"ifPeerReadMyLastMessage": 0,
"unReadCount": 0,
"roomMembersCount": 2,
"isMute": 0
}

on user_online_changed

  • this event fire if user become offline or online only in my chats list
  • res is in json format you need to decode it first
{
"status": 1,
"roomId": "xxx"
}

  • 1 for become online
  • 0 for offline

on user_typing_changed

  • res is in map format you don't need to decode it
{
"status": "",
"name": "",
"roomId": ""
}
  • status enum with this data ["stop","typing","recording"]
  • name name of user case this group chat
  • roomId room id which has the changes

Socket message name space

you need to connect this if user in message page and manually disconnect if he leaves

connect url is {{baseUrl}} without /api/v1/ so it will be http://170.178.195.150:81/message

you need to connect this socket when user enter the message page

  • connect data
{
'transports': ['websocket'],
'pingTimeout': 5000,
'connectTimeout': 5000,
'pingInterval': 5000,
'extraHeaders': <String, String>{
'Authorization': accessToken
},
'forceNew': true
}

accessToken No Bearer token just send the token you can get one from login or register socket can't connect if no accessToken provided or not valid access token send

emit join

  • pass roomId to its param
  • _socket.emit("join", currentRoomId);

on all_messages event

  • res is in json format you need to decode it
  • res will like this
[
{
"_id": "61cd922a3bd8731be040e138",
"messageType": "text",
"messageAttachment": null,
"createdAt": 1640862250273,
"content": "first",
"senderId": "61cd8fb33bd8731be040e103",
"senderName": "user1 updated name",
"senderImageThumb": "REGISTER_3c651b24-3912-4191-a131-af99292a75f9_THUMB.jpg",
"roomId": "61cd92223bd8731be040e11e"
},
{
"_id": "61cd92223bd8731be040e128",
"messageType": "text",
"messageAttachment": null,
"createdAt": 1640862242839,
"content": "first",
"senderId": "61cd8fb33bd8731be040e103",
"senderName": "user1 updated name",
"senderImageThumb": "REGISTER_3c651b24-3912-4191-a131-af99292a75f9_THUMB.jpg",
"roomId": "61cd92223bd8731be040e11e"
}
]

on new_message event

  • res is in json format you need to decode it
  • this event will invoke on evey message you send or receive will invoke for sender and receiver so update your ui one you receive
  • res will like this
 {
"_id": "61cd922a3bd8731be040e138",
"messageType": "text",
"messageAttachment": null,
"createdAt": 1640862250273,
"content": "first",
"senderId": "61cd8fb33bd8731be040e103",
"senderName": "user1 updated name",
"senderImageThumb": "REGISTER_3c651b24-3912-4191-a131-af99292a75f9_THUMB.jpg",
"roomId": "61cd92223bd8731be040e11e"
}

don't forget to disconnect from this name space after user leave the message page so i can make received message as un read and send notifications